package org.hawk.gwtppc.webtest.common;

import java.util.ArrayList;
import java.util.List;

import org.hawk.gwtppc.webtest.common.Asserts.SkipError;
/**
 * Test suite to group tests by common goal.
 * Also contains logic of tests execution.
 * 
 * @author alex.bereznevatiy@gmail.com
 */
public class TestSuite {
	
	private List<Test> tests;
	
	private String name;
	/**
	 * Creates testsuite
	 * @param name
	 * @param tests
	 */
	public TestSuite(String name, Test...tests) {
		if (name == null && !getClass().equals(TestSuite.class)) {
			name = getClass().getName();
		}
		this.name = name;
		this.tests = new ArrayList<Test>();
		for (Test test : tests) {
			addTest(test);
		}
	}
	/**
	 * Adds test to this suite.
	 * @param test to add
	 */
	public void addTest(Test test) {
		tests.add(test);
	}

	/**
	 * Execute testcases added to this suite.
	 * @param report to log the process
	 */
	void run(Report report) {
		report.startSuite(name);
		
		for (Test test : tests) {
			try {
				test.run();
				report.success(test);
			} catch (SkipError th) {
				report.skip(test, th.getMessage());
			} catch (Throwable th) {
				report.failure(test, th);
			}
		}
		
		report.endSuite();
	}

}
